package com.buho29.rcmanager;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.MenuItem.OnMenuItemClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.buho29.rcmanager.bd.DatabaseAdapter;
import com.buho29.rcmanager.bd.TableModel;
import com.buho29.rcmanager.bd.TablePowerItem;
import com.buho29.rcmanager.utils.Make;
import com.google.zxing.integration.android.IntentIntegrator;

public class PowerEdit extends Activity {
    
	private EditText mNameText;
    private EditText mDescriptionText;
	private EditText mVoltageText;
    private EditText mCapacityText;
    
    private Long mId;
	private int mCmd;
	
    public static final String CMD="cmd";
    
	private DatabaseAdapter mDbAdapter;
	private TablePowerItem mDbTablePowerItem = new TablePowerItem();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.power_edit);
        
        mDbAdapter = DatabaseAdapter.getInstance(this); 
        mDbTablePowerItem.setSQLiteDatabase(mDbAdapter.getDatabase());
        
        mNameText = (EditText) findViewById(R.id.tvName);
        mDescriptionText = (EditText) findViewById(R.id.tvDescription);
        mVoltageText = (EditText) findViewById(R.id.etVoltage);
        mCapacityText = (EditText) findViewById(R.id.etCapacity);
        
        Button confirmButton = (Button) findViewById(R.id.btSave);
       
        
		Bundle extras = getIntent().getExtras(); 
        if(savedInstanceState != null){
			mId =  savedInstanceState.getLong(TablePowerItem.COL_ROW_ID);
			mCmd = savedInstanceState.getInt(CMD);
		}else if(extras != null) {           
			mId =  extras.getLong(TablePowerItem.COL_ROW_ID);
			mCmd = extras.getInt(CMD);
		}
       
        confirmButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                saveData();
            }
            
        });
        
		populateFields();
    }   
    
    private void populateFields() {
        if (mCmd == TablePowerItem.CMD_EDIT) {
        	
            mDbTablePowerItem.setId(mId);
            mDbTablePowerItem.load();
            
            mNameText.setText(mDbTablePowerItem.getName());
            mDescriptionText.setText(mDbTablePowerItem.getDescription());
            
            mCapacityText.setText(String.valueOf(mDbTablePowerItem.getCapacity()));
            mVoltageText.setText(String.valueOf(mDbTablePowerItem.getVoltage()));
            
            
            setTitle(mDbTablePowerItem.getName());
        }else {
        	mDbTablePowerItem.setId(0L);
        }
    }
    
    private void saveData() {
        
        mDbTablePowerItem.setName(mNameText.getText().toString());
        mDbTablePowerItem.setDescription(mDescriptionText.getText().toString());
        mDbTablePowerItem.setCapacity(Integer.parseInt(mCapacityText.getText().toString()));
        mDbTablePowerItem.setVoltage(Float.valueOf(mVoltageText.getText().toString()));
        
        long result = mDbTablePowerItem.save();
        
        Intent intent = new Intent();
		intent.putExtra(TablePowerItem.COL_ROW_ID,result);
        
		Make.toast(PowerEdit.this,getText(R.string.saved)).show();
        setResult(RESULT_OK,intent);
        finish();
        
    }
    
    
    
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(R.string.encode_QR).setOnMenuItemClickListener(new OnMenuItemClickListener() {
			@Override
			public boolean onMenuItemClick(MenuItem item) {
				if(!Preferences.neverEncodingQr(PowerEdit.this))
					Preferences.setEncodingQr(PowerEdit.this);
				
				IntentIntegrator.shareText(PowerEdit.this, String.valueOf(mId));
				return true;
			}
		}).setEnabled(mCmd == TablePowerItem.CMD_EDIT);
		return true;
	}
    

	@Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putLong(TableModel.COL_ROW_ID, mId);
        outState.putLong(CMD, mCmd);
    }
    
}
